package com.study.controller;

import com.github.pagehelper.PageInfo;
import com.study.entity.*;
import com.study.service.IAppCategoryService;
import com.study.service.IAppInfoService;
import com.study.service.IDataDictionaryService;
import com.study.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2017/10/19/019.
 */
@Controller
@RequestMapping("app")
public class AppInfoController {
    @Autowired
    private IAppInfoService appInfoService;
    @Autowired
    private IAppCategoryService appCategoryService;
    @Autowired
    private IDataDictionaryService dataDictionaryService;

    @RequestMapping("list")
    public String list(Model model, AppInfo appInfo,
                       @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                       @RequestParam(value = "size", required = false, defaultValue = "2") Integer size) {
        List<AppInfo> list = appInfoService.findList(appInfo, page, size);
        PageInfo<AppInfo> pageInfo = new PageInfo<>(list);//将list包装到page中，自动算出总页数和其他参数
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("appInfo", appInfo);
        return "app/list";
    }

    @RequestMapping("getLevel2")
    @ResponseBody
    public List<AppCategory> getLevel2(Integer parentId) {
        return appCategoryService.findListByPid(parentId);
    }

    @RequestMapping("add")//约定大于配置原则   app/add
    public void add() {
    }//页面跳转


    @RequestMapping("doAdd")
    public String doAdd(Model model, AppInfo appInfo, @RequestParam(name = "img", required = false) MultipartFile multipartFile) throws IOException {
        //TODO 判断文件的属性
        String filePath = UUIDUtils.uuid() + multipartFile.getOriginalFilename();//获取文件名
        multipartFile.transferTo(new File("D:/upload/", filePath));//保存文件

        appInfo.setImgUrl(filePath);//设置文件路径
        appInfo.setSize((double) multipartFile.getSize());//设置文件大小

        int i = appInfoService.saveOne(appInfo);//保存文件
        if (i != 0) {
            return "redirect:list";//重定向到app列表页面
        } else {
            model.addAttribute("appInfo", appInfo);//用于数据回显
            return "app/add";//转发到添加页面
        }
    }

    @RequestMapping("dataDictionary")
    @ResponseBody
    public List<DataDictionary> dataDictionary(DataDictionary dataDictionary) {
        return dataDictionaryService.findList(dataDictionary);
    }

    @RequestMapping("update")
    public String update(Model model,Integer appId){
        AppInfo appInfo = appInfoService.findOneById(appId);
        model.addAttribute("appInfo",appInfo);
        return "app/update";
    }
    @RequestMapping("doUpdate")
    public String doUpdate(Model model,AppInfo appInfo, @RequestParam(name = "img", required = false) MultipartFile multipartFile) throws IOException {
        String filePath = UUIDUtils.uuid() + multipartFile.getOriginalFilename();//获取文件名
        multipartFile.transferTo(new File("D:/upload/", filePath));//保存文件

        appInfo.setImgUrl(filePath);//设置文件路径
        appInfo.setSize((double) multipartFile.getSize());//设置文件大小
        int i = appInfoService.updateOne(appInfo);
        if(i!=0){
            return "redirect:list";//重定向到app列表页面
        }else {
            model.addAttribute("appInfo", appInfo);//用于数据回显
            return "app/update";//转发到添加页面
        }
    }

    public String aa(){
        return null;
    }

    public void bb(){

    }
}
